<template>
  <a-select
    :mode="mode"
    :disabled="disabled"
    :allowClear="allowClear"
    @change="changeHandler"
    placeholder="请选择"
    v-model="innerValue"
    :getPopupContainer="triggerNode => triggerNode.parentNode"
  >
    <a-select-option :key="item.value" :value="item.value" v-for="item in options">
      {{ item.label }}
    </a-select-option>
  </a-select>

</template>

<script>
// 题库选择列表
export default {
  model: {
    prop: 'outValue',
    event: 'change'
  },
  props: {
    outValue: {
      required: true
    },
    disabled: {
      type: Boolean,
      default: false
    },
    mode: {
      type: String,
      default: function () {
        return 'default'
      }
    },
    allowClear: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      innerValue: null,
      options: []
    }
  },
  mounted () {
    const params = {
      _search: false,
      selectedType: 'question_source',
      type: 1
    }
    this.$axios
      .post(this.$URLMAP.url.exam.itemBank.query, params)
      .then((response) => {
        if (response.code === 200) {
          this.options = response.data.map(({ examId, examName }) => {
            return {
              label: examName,
              value: examId
            }
          })
          if (this.mode === 'default') {
            if (this.outValue == null || this.outValue.length === 0) {
              this.innerValue = ''
            } else {
              this.innerValue = this.outValue
            }
          } else {
            if (this.outValue == null || this.outValue.length === 0) {
              this.innerValue = []
            } else {
              this.innerValue = this.$lodash.split(this.outValue, ',')
            }
          }
        }
        this.$emit('change', this.innerValue)
      })
  },
  methods: {
    changeHandler (input) {
      if (input == null || input.length === 0) {
        this.$emit('change', null)
      } else {
        if (this.mode === 'multiple') {
          this.$emit('change', this.$lodash.join(input, ','))
        } else {
          this.$emit('change', input)
        }
      }
    }
  }
}
</script>
